<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>JS + CSS 实现免阻塞遮罩</title>
    <style>
      * {
        padding: 0px;
        margin: 0px;
      }
      .mask {
        position: absolute;
        top: 0px;
        right: 0px;
        bottom: 0px;
        left: 0px;
        background-color: black;
        opacity: 0.5;
        animation-name: mask;
        animation-duration: 1s;
        animation-timing-function: linear;
        animation-iteration-count: 2;
        /* 来回进行切换 */
        animation-direction: alternate;
      }
      @keyframes mask {
        50% {
          background-color: burlywood;
        }
        to {
          background-color: black;
        }
      }
      body {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <button onclick="remoteRequest()">这是一个测试遮罩按钮</button>
  </body>
  <script>
    function openMask(callback) {
      let div = document.createElement('div')
      div.classList.add('mask')
      document.body.appendChild(div)
      div.addEventListener('animationend', () => {
        callback && callback(div)
        callback || div.remove()
      })
    }
    function remoteRequest() {
      openMask((div) => {
        if (confirm('关闭遮罩')) {
          div.remove()
        }
      })
    }
  </script>
</html>
